/**
 * 用户基本信息
 * Created by beansoft on 2017/6/9.
 */
import {KEY_USER_TOKEN, KEY_USER_INFO} from '../config';
import './Preferences';

let UserInfoStore = {};
let KEY_JPUSH_ID = "KEY_JPUSH_ID";
let LAST_USER_PHONE = "LAST_USER_PHONE";// 上次登陆后的用户手机号
let UPGRADE_ALERT = "UPGRADE_ALERT";//版本更新弹窗
let UPGRADE_SETTING = "UPGRADE_SETTING";//版本更新设置new
let KEY_AUTHORITY_CODE = "KEY_AUTHORITY_CODE";//版本更新设置new

UserInfoStore.getAuthorityCode = async function (): Object {
    let value = await Preferences.get(KEY_AUTHORITY_CODE);
    if (value !== null) {
        return JSON.parse(value);
    }
    return value;
};

UserInfoStore.setAuthorityCode = async function (value: Object) {
    return Preferences.set(KEY_AUTHORITY_CODE, JSON.stringify(value));
}

UserInfoStore.removeAuthorityCode = async function () {
    return Preferences.remove(KEY_AUTHORITY_CODE);
}

UserInfoStore.getUserInfo = async function (): Object {
    let value = await Preferences.get(KEY_USER_INFO);
    if (value !== null) {
        console.log('getUserInfo', value);
        return JSON.parse(value);
    }
    return value;
};

UserInfoStore.setUserInfo = async function (value: Object) {
    return Preferences.set(KEY_USER_INFO, JSON.stringify(value));
}

UserInfoStore.removeUserInfo = async function () {
    return Preferences.remove(KEY_USER_INFO);
}

UserInfoStore.getUserToken = async function () {
    return await Preferences.get(KEY_USER_TOKEN);
}

UserInfoStore.setUserToken = async function (value: string) {
    return Preferences.set(KEY_USER_TOKEN, value);
};

UserInfoStore.removeUserToken = async function () {
    return Preferences.remove(KEY_USER_TOKEN);
}

UserInfoStore.getLastUserPhone = async function (): Object {
    return await Preferences.get(LAST_USER_PHONE);
};

UserInfoStore.setLastUserPhone = async function (value: string) {
    return Preferences.set(LAST_USER_PHONE, value);
};

UserInfoStore.removeLastUserPhone = async function () {
    return Preferences.remove(LAST_USER_PHONE);
}

UserInfoStore.getJPushID = async function () {
    return await Preferences.get(KEY_JPUSH_ID);
}

UserInfoStore.setJPushID = async function (value: string) {
    return Preferences.set(KEY_JPUSH_ID, value);
};
//版本更新弹窗
UserInfoStore.getUpgrade_alert = async function () {
    let value = await Preferences.get(UPGRADE_ALERT);
    if (value !== null) {
        console.log('getUpgrade_alert', value);
        return JSON.parse(value);
    }
    return value;
};

UserInfoStore.setUpgrade_alert = async function (value: object) {
    return Preferences.set(UPGRADE_ALERT, JSON.stringify(value));
};

UserInfoStore.removeUpgrade_alert = async function () {
    return Preferences.remove(UPGRADE_ALERT);
};

//版本更新设置new
UserInfoStore.getUpgrade_setting = async function () {
    let value = await Preferences.get(UPGRADE_SETTING);
    if (value !== null) {
        console.log('getUpgrade_alert', value);
        return JSON.parse(value);
    }
    return value;
};

UserInfoStore.setUpgrade_setting = async function (value: object) {
    return Preferences.set(UPGRADE_SETTING, JSON.stringify(value));
};

UserInfoStore.removeUpgrade_setting = async function () {
    return Preferences.remove(UPGRADE_SETTING);
};

global.UserInfoStore = UserInfoStore;// 全局可用
